/**
 * AspectSense project
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, started at the University of Cyprus
 * [http://www.ucy.ac.cy].
 *
 * More information about the project is available at
 * [http://code.google.com/p/aspectsense]
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.aspectsense.example_apps.multipleUI;

import org.aspectsense.annotations.Component;
import org.aspectsense.annotations.RequiredService;
import org.aspectsense.annotations.Service;
import org.aspectsense.annotations.ServiceUtility;
import org.aspectsense.framework.DefaultComponent;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p/>
 * Date: Jul 13, 2007<br/>
 * Time: 2:29:54 PM
 * </p>
 *
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 */
@Component(
        displayID = "MultipleUIAssistant"
)
@Service(
        serviceType = AssistantService.class
)
@ServiceUtility(
        targetServiceType = AssistantService.class,
        utilityFunction = "utility[org.aspectsense.example_apps.multipleUI.UIService]"
)
public class MultipleUIAssistant extends DefaultComponent implements AssistantService
{
    @RequiredService(
            variationPointID = "sample_app1.uiService"
    )
    private UIService uiService;

    private boolean isSuspended;

    private Thread myThread = null;

    public void activate()
    {
        isSuspended = false;

        myThread = new Thread(new Worker());
        myThread.start();
    }

    public void suspend()
    {
        isSuspended = true;

        // synchronized, to block method until the Thread is suspended
        synchronized(this) {}

        // dereference
        myThread = null;
    }

    private class Worker implements Runnable
    {
        public void run()
        {
            while(!isSuspended)
            {
                synchronized(this)
                {
                    try
                    {
                        Thread.sleep(2000);
                    }
                    catch(InterruptedException ie)
                    {
                        System.out.println("ie: " + ie.getMessage());
                    }

                    SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm:ss");
                    SimpleDateFormat weekdayDateFormat = new SimpleDateFormat("EEEE");
                    Date date = new Date();

                    uiService.communicateText("Hello there! The time is "
                            + timeDateFormat.format(date) + " and the day is "
                            + weekdayDateFormat.format(date) + ".");
                }
            }
        }
    }
}